package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.CollectionKit;
import com.blade.kit.StringKit;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Announcement;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.roles.AdminRoles;
import com.wxxymaker.edu.roles.IgnoreAuth;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.roles.TeacherRoles;
import com.wxxymaker.edu.service.AnnouncementService;
import com.wxxymaker.edu.utils.TimeUtils;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @author zyx
 * @date 2018/9/14 - 21:37
 * 公告发布操作相关操作接口
 */
@Path(value = "/user/teacher/announcement", restful = true)
public class AnnouncementHandlerApi {
    @Inject
    private AnnouncementService announcementService;

    /**
     * 获取管理员发布的分页公告
     */
    @IgnoreAuth
    @PostRoute("/anns")
    public RestResponse fetchAnns(@Param(defaultValue = "1", name = "page") int pageNo,
                                  @Param(defaultValue = "10") int limit,
                                  @Param(defaultValue = "false") Boolean all) {
         Page<Announcement> page = announcementService.getSysAnn(pageNo, limit, all);

        if (CollectionKit.isNotEmpty(page.getRows())) {
            return RestResponse.ok(page);
        }
        return RestResponse.fail();
    }

    /**
     * 管理员发布公告
     * @param announcement 公告信息 公告中的课程和任务id都为-9
     */
    @AdminRoles
    @PostRoute("/add")
    public RestResponse addAdminAnn(Announcement announcement) {
        User user = TokenUtils.currentUser();
        announcement.setUserId(user.getId());
        announcement.setCourseId(-9);
        announcement.setTaskId(-9);
        if (StringKit.isNotEmpty(announcement.getContent())) {
            announcement.setTime(TimeUtils.getDate());
            Announcement add = announcementService.add(announcement);
            if (add != null) {
                return RestResponse.ok(add);
            }
        }
        return RestResponse.fail();
    }

    @TeacherRoles
    @PostRoute("/showAnn")
    public RestResponse getAnns(@Param(defaultValue = "1") int page,
                               @Param(defaultValue = "10") int size,
                               @Param(name = "limit", defaultValue = "0") Integer limit) {
        if (limit > 0) size = limit;
        Integer uid = TokenUtils.currentUser().getId();
        Page<Announcement> announcementPage = announcementService.getByUid(uid, page, size);
        if (announcementPage.getTotalRows() > 0) {
            return RestResponse.ok(announcementPage);
        }
        return RestResponse.fail();
    }

    @StudentRoles
    @PostRoute("/:kid/showAnnByKid")
    public RestResponse getAnn(@Param(defaultValue = "1") int page,
                               @Param(defaultValue = "5") int size,
                               @PathParam Integer kid) {
        Page<Announcement> announcementPage = announcementService.getByKid(kid, page, size);
        if (announcementPage.getTotalRows() > 0) {
            return RestResponse.ok(announcementPage);
        }
        return RestResponse.fail();
    }

    @StudentRoles
    @PostRoute("/showAnnById")
    public RestResponse getAnn(@Param Integer id) {
        Announcement announcement = announcementService.showById(id);
        if (announcement != null) {
            return RestResponse.ok(announcement);
        }
        return RestResponse.fail();
    }

    @TeacherRoles
    @PostRoute("/deleteAnn")
    public RestResponse deleteAnn(@Param Integer id) {
        boolean flag = announcementService.deleteByid(id);
        if (flag) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 公告的添加
     * @return
     */
    @TeacherRoles
    @PostRoute("/publishAnn")
    public RestResponse publishAnn(@Param Announcement announcement) {
        User user = TokenUtils.currentUser();
        announcement.setUserId(user.getId());
        announcement.setTime(TimeUtils.getTime());
        Announcement add = announcementService.add(announcement);
        Page<Announcement> adds = announcementService.getByAid(add.getId());
        if(adds.getTotalRows() > 0) {
            return RestResponse.ok(adds);
        }
        return RestResponse.fail();
    }

    @TeacherRoles
    @PostRoute("/modifyAnn")
    public RestResponse modifyAnn(@Param Announcement announcement) {
        Integer i = announcementService.updateById(announcement);
        if (i > 0) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

}
